package com.cy.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

@RestController
@RequestMapping("/consumer/")
public class RestConsumerController {

    @Autowired
    private RestTemplate loadBalancedRestTemplate;
    @DeleteMapping("{id}")
    public  String doDeleteById(@PathVariable Integer id){
        String url = String.format("http://%s/provider/%s", "sca-provider",id);
        //loadBalancedRestTemplate.delete(url,id);
        //return "delete Ok";
        ResponseEntity<String> exchange = loadBalancedRestTemplate.exchange(url, HttpMethod.DELETE, null, String.class);
        System.out.println(exchange.getBody().getClass().getName());
        return exchange.getBody();
    }

    @PostMapping
    public Map<String,Object> doCreate(@RequestBody Map<String,Object> map){
        String url=String.format("http://%s/provider/","sca-provider");
        return loadBalancedRestTemplate.postForObject(url, map, Map.class);
    }

    @PutMapping
    public Map<String,Object> doupdata(@RequestBody Map<String,Object> map){
        String url=String.format("http://%s/provider/","sca-provider");
//        loadBalancedRestTemplate.put(url, map);
        ResponseEntity<Map> exchange = loadBalancedRestTemplate.exchange(url, HttpMethod.PUT, new HttpEntity(map), Map.class);
        return exchange.getBody();
//        return "updata ok";
    }


}
